const {
  curPopularActivityList,
  historyPopularActivityList
} = require('../../../../api/home');
const { filePreview } = require('../../../../api/index');

const {
  isFinished,
  sliceString
} = require('../../../../utils/util');

//  记录弹窗
let model = null;

// pages/home/index.js
Page({
  /*** 页面的初始数据 */
  data: {
    page: {
      pageNumber: 1,
      pageSize: 20
    },
    total: 0, //  总条数
    finished: false,
    loading: false,
    dataList: [],
    tabs: [{
        name: '当前活动',
        code: '当前活动'
      },
      {
        name: '历史活动',
        code: '历史活动'
      }
    ],
    curTab: '当前活动',
    list: [], // 当前活动列表
  },
  onChangeTabs(event) {
    const {
      code
    } = event.detail;

    this.setData({ curTab: code }, () => {
      this.data.page.pageNumber = 1;
      this.data.finished = false;
      this.data.list = [];
      this.setData({ list: [] }, () => {
        fecthList.call(this);
      })
    });
  },
  async onFace() {
    await wx.checkIsSupportSoterAuthentication({
      checkAliveType: 2,
      success(res) {
        console.log(res, 'res');
      }
    })
    const res = await wx.startSoterAuthentication({
      requestAuthModes: ['facial'],
      challenge: '123456',
      authContent: '请用指纹解锁',
      success(res) {
        console.log(res);
      }
    });
  },
  loadMore() {  //  加载更多
    fecthList.call(this);
  },
  /*** 生命周期函数--监听页面加载 */
  async onLoad(options) {
    fecthList.call(this);
  },
  /*** 生命周期函数--监听页面初次渲染完成 */
  onReady() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

//  获取列表数据
async function fecthList() {
  switch(this.data.curTab) {
    case '当前活动':
      fetchCurPopularActivityList.call(this);
      break;
    case '历史活动':
      fetchHistoryPopularActivityList.call(this);
      break;
  }
}

//  获取当前活动热门活动列表
async function fetchCurPopularActivityList() {
  wx.showLoading({
    title: '加载中...'
  });
  const {
    errors,
    result,
    totalCount
  } = await curPopularActivityList(this.data.page);
  if (errors.lenght > 0) {
    return;
  }

  //  判断数据是不是都已经加载完了
  this.setData({
    finished: isFinished(totalCount, this.data.page.pageNumber * this.data.page.pageSize)
  });
  //  加载更多数据
  this.setData({
    list: this.data.list.concat(setResult(result))
  });
  this.data.page.pageNumber++;

  wx.hideLoading();
}

//  获取历史活动列表
async function fetchHistoryPopularActivityList() {
  wx.showLoading({ title: '加载中...' });
  const {
    errors,
    result,
    totalCount
  } = await historyPopularActivityList(this.data.page);
  if (errors.lenght > 0) {
    return;
  }

  //  判断数据是不是都已经加载完了
  this.setData({
    finished: isFinished(totalCount, this.data.page.pageNumber * this.data.page.pageSize)
  });
  //  加载更多数据
  this.setData({
    list: this.data.list.concat(setResult(result))
  });
  this.data.page.pageNumber++;

  wx.hideLoading();
}

//  处理数据
function setResult( data = [] ) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.subtitle = sliceString( item.subtitle, 14 );
    item.activityPhoto = filePreview( '/file/preview/' + item.activityPhoto );
    return item;
  })

  return _data;
}